<template>
  <a-card :bordered="false">
    <div>
      <a-form @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="12" :sm="12">
            <a-form-item label="车牌" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input v-model="queryParam.plate" placeholder="请输入车牌"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-item>
              <a-button type="primary" icon="search" @click="searchQuery()">查询</a-button>
              <a-button type="primary" icon="reload" style="margin-left: 8px" @click="searchReset()">重置</a-button>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <vxe-grid
        id="PlateList"
        ref="vxeTable"
        :row-config="{keyField:'plate'}"
        v-bind="options"
        v-on="listeners"
    >
      <template #action="{ row }">
        <a @click="handleEdit(row)">编辑</a>
        <a-divider type="vertical" />
        <a-dropdown>
          <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a @click="handleDetail(row)">详情</a>
            </a-menu-item>
            <a-menu-item>
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(row.plate)">
                <a>删除</a>
              </a-popconfirm>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </template>
      <template #enable="{ row }">
        <a-switch v-model="row.enable" @change="enableChange(row)"/>
      </template>
    </vxe-grid>
    <PlateModal @ok="modalFormOk" ref="modalForm" />
  </a-card>
</template>

<script>
import { VxeTableMixin } from '@/mixins/VxeTableMixin'
import PlateModal from './modules/PlateModal.vue'
import { getAction } from '@/services/manage'
export default {
  name: 'PlateList',
  mixins: [VxeTableMixin],
  components: {
    PlateModal
  },
  data() {
    return {
      imageContent: '',
      columns: [
        { type: 'checkbox', width: 40, fixed: 'left' },
        { type: 'seq', width: 40, fixed: 'left' },
        { title: '车牌', field: 'plate',width: 100 },
        { title: '生效时间', field: 'enableTime',width: 250 },
        { title: '失效时间', field: 'overdueTime', width: 250 },
        { title: '备注', field: 'context', width: 250 },
        { title: '状态', field: 'enable', width: 100, slots: { default: 'enable' }},
        { title: '操作', field: 'operation', width: 150, fixed: 'right', slots: { default: 'action' }}
      ],
      url: {
        list: '/stop/v1/page',
        delete: '/stop/v1/remove/',
        deleteBatch: '/stop/v1/remove/',
        enable: '/stop/v1/enableOrDisable/',
        openHatch: '/stop/v1/open',
        snapshot: '/stop/v1/snapshot'
      },
      customTableToolbar: {
        import: false,
        buttons: [
          { buttonRender: { name: 'VxeToolbarAdd', events: { click: this.handleAdd }, disabled: this.disableSubmit }},
          { buttonRender: { name: 'VxeToolbarDel', events: { click: this.batchDel }, disabled: this.disableSubmit }},
          { buttonRender: {
            name: 'VxeToolbarAdd',
            events: { click: this.openHatch },
            toolbarProps: {
              title: '开闸',
              icon: 'unlock'
            }
          }},
          { buttonRender: {
            name: 'VxeToolbarAdd',
            events: { click: this.snapshot },
            toolbarProps: {
              title: '抓图',
              icon: 'camera'
            }
          }}
        ]
      },
      elementId: 'build7:plate:export'
    }
  },
  mounted() {
    this.queryParam.plate = '豫'
    this.loadData(1)
  },
  methods: {
    enableChange(row) {
      this.loading = true
      getAction(`${this.url.enable}${row.plate}`).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData('query')
        }
      }).finally(() => {
        this.loading = false
      })
    },
    openHatch() {
      this.loading = true
      getAction(`${this.url.openHatch}`).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData('query')
        }
      }).finally(() => {
        this.loading = false
      })
    },
    snapshot() {
      this.loading = true
      getAction(`${this.url.snapshot}`).then((res) => {
        if (res.success) {
          const image = new Image()
          image.src = 'data:image/png;base64,' + res.result;
          image.style.margin = '0 auto'
          image.style.display = 'block'
          image.style.width = '100%'
          image.style.height = '100%'
          const newWin = window.open('', '_blank')
          newWin.document.write(image.outerHTML)
        }
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>
